/* 
Copyright (c) 2004,2005, 2006, 2007, 2008 University of Illinois 

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/** @fileoverview
 * This helps with managing rdf url's for other functions in this extension.
 */
AccessibilityExtension.ns(function() { with (AccessibilityExtension.LIB) {

	var AE = AccessibilityExtension.ns;  
	/** Where the "cached" rdf files are located */
	var baseURL = "chrome://accessext/content/rdf/";
	
	/** Translates the urls from their url form to our filenaming conventions */
	var cachedRDF = new Array();
	
	cachedRDF["http://www.w3.org/1999/02/22-rdf-syntax-ns"] = baseURL+"www.w3.org_1999_02_22-rdf-syntax-ns.rdf";
	cachedRDF["http://www.w3.org/2000/01/rdf-schema"] = baseURL+"www.w3.org_2000_01_rdf-schema.rdf";
	cachedRDF["http://dublincore.org/2003/03/24/dces"] = baseURL+"dublincore.org_2003_03_24_dces.rdf";		
	cachedRDF["http://www.w3.org/2002/07/owl"] = baseURL+"www.w3.org_2002_07_owl.rdf";	
	
	cachedRDF["http://www.w3.org/2004/02/skos/core"] = baseURL+"www.w3.org_2004_02_skos_core_history_2006-04-18.rdf";
	
	// More URI's than URL's, don't even try to resolve them
	cachedRDF["http://xmlns.com/foaf/0.1/"] = baseURL+"";
	cachedRDF["http://www.w3.org/2001/XMLSchema"] = baseURL+"";	
	
	// This seems to be redirected to the most up-to-date version of DC, so we'll always follow it
	//	cachedRDF["http://purl.org/dc/elements/1.1/"] = baseURL+"";
	
	
	// These namespaces don't resolve, so we need to redirect them to our internal versions
	cachedRDF["http://www.w3.org/2005/01/wai-rdf/GUIRoleTaxonomy"] = baseURL+"www.w3.org_2005_01_wai-rdf_GUIRoleTaxonomy.rdf";	
	cachedRDF["http://www.w3.org/2005/01/wai-rdf/schemas/accessibility"] = baseURL+"";	
	//cachedRDF["http://www.w3.org/2005/01/aaa"] = baseURL+"";
	cachedRDF["http://www.w3.org/2005/07/aaa"] = baseURL+"";
	
	// URL's for our test pages?
	// FIXME: We don't really need these because we're already redirecting the "official" spec rdf's in the cache to our versions
	// cachedRDF["http://cita.uiuc.edu/2006/06/role"] = baseURL+"www.w3.org_2005_01_wai-rdf_GUIRoleTaxonomy.rdf";
	// cachedRDF["http://cita.uiuc.edu/2006/06/state"] = baseURL+"";
	 
AccessibilityExtension.LIB.extend(AccessibilityExtension.LIB,
{
	/** Returns a URL to the cached RDF definition if possible, or the original URL if it is not contained in the cache.
	 * @param originalURL a likely RDF URL
	 * @returns the best URL to access
	 */
	rdfGetCachedURL: function(originalURL) {
		originalURL = cleanURL(originalURL);
		if(typeof cachedRDF[originalURL] != "undefined") {
			return cachedRDF[originalURL];
		}
		return originalURL;
	},
	
	/** Returns a URL to the original RDF definition if possible, or the given cached URL if it is not found in the cache.
	 * @param cachedURL a URL from the cache
	 * @returns the original, pre-cache URL
	 */
	rdfGetOriginalURL: function(cachedURL) {
		cachedURL = cleanURL(cachedURL);
		for(var i in cachedRDF) {
			if( cachedRDF[i] == cachedURL ) {
				return i;			
			}
		}
		return cachedURL;
	},
	
	/** For resources that have their resource namespaces altered because they were loaded from the cache, 
	 * this re-creates the original resource namespace
	 * @param resource an RDF resource URI
	 * @returns the original : function(non-cached if possible) version of the namespace.
	 */
	rdfGetOriginalResource: function(resource) {
		return rdfGetOriginalURL(rdfGetNamespace(resource)) + "#" + rdfGetName(resource);
	},
	
	/** Removes the trailing octothorpe from a URL so it may be compared
	 * @param urlStr a URL
	 * @returns the given URL without a trailing octothorpe
	 */
	cleanURL: function(urlStr) {
		if(urlStr[urlStr.length-1] == "#") {
			urlStr = urlStr.slice(0,urlStr.length-1);
		}
		return urlStr;
	},
	
	/** Get the namespace of an RDF resource. 
	 * Given "xxx://foo.bar/bat#baz", this would return everything but "baz"
	 * @see #rdfGetName
	 * @param resource an RDF resource string
	 * @returns the namespace portion of the resource or an empty string if no namespace is detected
	 */
	rdfGetNamespace: function(resource) {
		var ns = resource.match(/(^.*:\/\/[^#]*)*#*(.*)*/); // namespace is in index 1, name is in index 2, if they exist
		if( ns[1] != null ) {
			return (ns[1]);
		} else {
			return("");
		}
	},
	
	/** Get the target or local name of an RDF resource. 
	 * Given "xxx://foo.bar/bat#baz", this would "baz"
	 * @see #rdfGetNamespace
	 * @param resource an RDF resource string
	 * @returns the name/target portion of the resource or an empty string if not found
	 */
	rdfGetName: function(resource) {
		var ns = resource.match(/(^.*:\/\/[^#]*)*#*(.*)*/); // namespace is in index 1, name is in index 2, if they exist
		if( ns[2] != null ) {
			return (ns[2]);
		} else {
			return("");
		}
	}
});
}});	
